//
//  BGSellerGoBidCell.m
//  bigMall
//
//  Created by LTOVE on 16/9/24.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGSellerGoBidCell.h"
#import "BGSellerGoBidCellModel.h"
static NSString *const reuseIdentidy = @"cell";
@interface BGSellerGoBidCell ()<UITextFieldDelegate>

@end

@implementation BGSellerGoBidCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(BGSellerGoBidCellModel *)model
{
    _model = model;
    [self setUpAllSubViewsWithModel:model];
    
}

- (void)setUpAllSubViewsWithModel:(BGSellerGoBidCellModel *)model

{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    UIView *contentView = [[UIView alloc]initWithFrame:self.bounds];
    [self.contentView addSubview:contentView];
    if ([model isKindOfClass:[BGSellerGoBidCellModelTextFild class]]) {
        //文本框
        BGSellerGoBidCellModelTextFild *itemModel = (BGSellerGoBidCellModelTextFild *)model;
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = [NSString stringWithFormat:@"%@:",itemModel.title];
        titleLabel.font = Font34;
        titleLabel.textColor = textBlackColor;
        titleLabel.x = BGMaginLeft;
        [titleLabel sizeToFit];
        titleLabel.centerY = contentView.height * 0.5;
        [contentView addSubview:titleLabel];
        UITextField *textFild = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame) + 5, titleLabel.y, screenW - 2 * BGMaginLeft - 5 - titleLabel.width, titleLabel.height)];
        textFild.placeholder = itemModel.placehoder;
        textFild.text = itemModel.textFildStr;
        if (itemModel.indexPath.row != 2) {
            textFild.keyboardType = UIKeyboardTypeDecimalPad;
        }
        
        textFild.delegate = self;
        textFild.font = Font30;
        textFild.textColor = textGrayColor;
        [contentView addSubview:textFild];
    }else if ([model isKindOfClass:[BGSellerGoBidCellModelTextCell class]]){
        //总价什么的
        BGSellerGoBidCellModelTextCell *itemModel = (BGSellerGoBidCellModelTextCell *)model;
        UILabel *totalPriceLabel = [self labelWithTitle:@"总价: " color:textBlackColor];
        UILabel *totalPriceValueLabel = [self labelWithTitle:itemModel.totalPrice color:read_];
        UILabel *maginLabel = [self labelWithTitle:@"需付押金: " color:textBlackColor];
        UILabel *maginLabelValue = [self labelWithTitle:itemModel.magin color:read_];
        totalPriceLabel.centerY = totalPriceValueLabel.centerY = maginLabel.centerY = maginLabelValue.centerY = contentView.height * 0.5;
        totalPriceLabel.x = BGMaginLeft;
        totalPriceValueLabel.x = CGRectGetMaxX(totalPriceLabel.frame) + 5;
        maginLabel.x = screenW - 2 * BGMaginLeft - 5 - maginLabelValue.width - maginLabel.width;
        maginLabelValue.x = CGRectGetMaxX(maginLabel.frame) + 5;
        [contentView addSubview:totalPriceLabel];
        [contentView addSubview:totalPriceValueLabel];
        [contentView addSubview:maginLabel];
        [contentView addSubview:maginLabelValue];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(cellValueDidChangedWithModel:)]) {
        BGSellerGoBidCellModelTextFild *model =(BGSellerGoBidCellModelTextFild *) self.model;
        model.textFildStr = textField.text;
        [_delegate cellValueDidChangedWithModel:model];
    }
}

- (UILabel *)labelWithTitle:(NSString *)title color:(UIColor *)color
{
    UILabel *label = [UILabel new];
    label.text = title;
    label.textColor = color;
    [label sizeToFit];
    label.font = Font34;
    return label;
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    BGSellerGoBidCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentidy];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentidy];
    }
    return cell;
}

@end
